package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[分页查询]data:{}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> pages= permissionDao.findPage(queryPageBean.getQueryString());
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 更新
     * @param permission
     */
    @Override
    @Transactional
    public void update(Permission permission) {
        log.info("[更新]data:{}",permission);
        permissionDao.update(permission);
    }

    /**
     * 添加权限
     * @param permission
     */
    @Override
    @Transactional
    public void add(Permission permission) {
        log.info("[添加]data:{}",permission);
        permissionDao.insert(permission);
    }

    /**
     * 更新前的查询
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        log.info("【更新前查询】:data{}",id);
        return   permissionDao.selectById(id);

    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        log.info("[删除]data:{}", id);
        //先删关联表
        permissionDao.deletePerssionAndRoleById(id);
        permissionDao.deleteById(id);
    }
    /**
     * 编辑窗口页面权限信息
     */
    @Override
    public List<Permission> findAll() {
        return  permissionDao.selectAll();
    }
}
